//
//  LGCommentViewController.m
//  LuxuryGoods
//
//  Created by DCQ on 2018/7/24.
//  Copyright © 2018年 Future. All rights reserved.
//

#import "LGCommentViewController.h"
#import "LGPlaceholderTextView.h"
#import "UserInfoModel.h"
#import "AllOrdersListViewController.h"
#import "IdAndMaOrderListViewController.h"
#import "MessageListViewController.h"

@interface LGCommentViewController ()<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet LGPlaceholderTextView *textView;
@property (weak, nonatomic) IBOutlet UILabel *fontNumberLabel;
@property (copy, nonatomic)  NSString * orderId;
@property (assign, nonatomic) LGCommentVCType type;
@end

@implementation LGCommentViewController
- (instancetype)initWithOrderId:(NSString *)orderId withVCType:(LGCommentVCType )type{
    self = [super init];
    if (self) {
        self.orderId = orderId;
         self.type = type;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setNavigationItemTitle:@"待评价"];
    self.textView.placeholder = @"请填写评价内容";
    self.textView.delegate = self;
    
}
//正在改变

- (void)textViewDidChange:(UITextView *)textView

{
    NSLog(@"%@", textView.text);
    
    //实时显示字数
    self.fontNumberLabel.text = [NSString stringWithFormat:@"%lu/200", (unsigned long)textView.text.length];
    //字数限制操作
    if (textView.text.length >= 200) {
        
        textView.text = [textView.text substringToIndex:200];
        
        self.fontNumberLabel.text = @"200/200";
        
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)submitAction:(id)sender {
    
    [self requestAddEval];
}

- (void)requestAddEval{
    if (!self.textView.text && self.textView.text.length == 0) {
        
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"温馨提示"
                                                                       message:@"请您输入评价内容"
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
                                                              handler:nil];
        
        
        [alert addAction:defaultAction];
        
        [self presentViewController:alert animated:YES completion:nil];
        return ;
    }
    NSDictionary * params = @{@"order_id":self.orderId,@"token":UserInfo.userToken,@"eval_cont":self.textView.text};
    NSString* pathStr = @"";
    if (self.type == LGCommentVCType_order) {
       pathStr = @"/Order/addEval";
    }else if(self.type == LGCommentVCType_idOrMatn){
        pathStr= @"Otherorder/addEval";
    }
    [[MainApi sharedInstance] postPath:pathStr params:params resultBlock:^(NSDictionary *responseObject, NSError *error) {
 
        NSLog(@"%@===",responseObject);
        if (!error) {
            NSString * NotificationName = @"";
            
            if (self.fromVCType == CommentFromVCType_Msg) {
                NotificationName = @"UPDATE_MESSAGE_LIST";
            }else{
                if ( self.type == LGCommentVCType_idOrMatn) {
                    NotificationName = @"UPDATE_IdAndMaOrder_LIST";
                }else if ( self.type == LGCommentVCType_order) {
                    NotificationName = @"UPDATE_ORDER_LIST";
                }
                
            }
            [[NSNotificationCenter defaultCenter] postNotificationName:NotificationName object:nil];
            
            for (UIViewController * tempVC in self.navigationController.viewControllers) {
                if ([tempVC isKindOfClass:[AllOrdersListViewController class]]) {
                    [self.navigationController popToViewController:tempVC animated:YES];
                }
                if ([tempVC isKindOfClass:[IdAndMaOrderListViewController class]]) {
                    [self.navigationController popToViewController:tempVC animated:YES];
                }
                if ([tempVC isKindOfClass:[MessageListViewController class]]) {
                    [self.navigationController popToViewController:tempVC animated:YES];
                }
            }
           
        }
        
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
